﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Tulde.WindowsPhone.Music.ViewModel;
using Tulde.WindowsPhone.Music;

namespace MP3tunes.LockerPlayer.Pages
{
    public partial class AlbumPage : PhoneApplicationPage
    {
        protected Artist SelectedArtist { get; private set; }


        public AlbumPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(AlbumPage_Loaded);
        
        }

        void AlbumPage_Loaded(object sender, RoutedEventArgs e)
        {
            this.statusProgress.IsIndeterminate = true;
            MusicServiceFactory.Instance.GetArtistAlbums(this.SelectedArtist, albums =>
            {

          
                this.listAlbums.DataContext = albums.Albums;
                this.statusProgress.IsIndeterminate = false;
                this.listAlbums.IsEnabled = true;
                this.listAlbums.SelectionChanged+=new SelectionChangedEventHandler(listAlbums_SelectionChanged);
                //this.progressBar1.IsIndeterminate = false;

            });

            //this.pivotControl.FadeIn(OpacityAnimation.Normal);
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            
                 base.OnNavigatedTo(e);
                this.SelectedArtist = NavigationContext.GetViewModelFromQueryParameters<Artist>();
                this.artistLabel.Text = this.SelectedArtist.ArtistName;
                this.listAlbums.IsEnabled = false;
               
           

        }
        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);
        

        }

        private void listAlbums_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.listAlbums.SelectedIndex >= 0)
            {
                Album album = this.listAlbums.SelectedItem as Album;
                if (album != null)
                {
                    //this.pivotControl.FadeOut(OpacityAnimation.Normal, (x, y) =>
                    //{
                        
                        NavigationService.NavigateWithViewModels("/Pages/MediaPlayer.xaml", album);
                    //});
                }
            }

        }

        private void AppMenu_OnNavigate(object sender, UserControls.AppMenuEventArg e)
        {

            this.pivotControl.FadeOut(OpacityAnimation.Normal, (x, y) =>
            {
                this.NavigationService.NavigateWithViewModels(e.Destination);
            });
        }

        private void Image_ImageOpened(object sender, RoutedEventArgs e)
        {
            var image = sender as Image;
           // image.SetValue(BackgroundProperty, Colors.Red);
        }
    }
}